/**
 * @author jinguangguo
 * @date 2018/1/30
 */
import mathTool from '../../../common/mathTool';
import passwordDialog from '../passwordDialog.html';
import confirm from "../../confirm/confirm";
import config from "../config";

class ListController {
    /**
     *
     * @param $scope
     */
    constructor($scope, settingsSvc, $uibModal, $location, $cookies) {
        "ngInject";

        this.$scope = $scope;
        this.service = settingsSvc;
        this.$uibModal = $uibModal;
        this.$location = $location;
        this.$cookies = $cookies;

        this.config = config;
        this.isLoading = true;
        this.coinListMap = {};

        this.levels = [0,1,2];
        this.limitList_level0 = {};
        this.limitList_level1 = {};
        this.limitList_level2 = {};

        this.init();

    }

    init() {
        this._queryList(() => {
            this._initLimitList();
            this._fetchLimitListByLevel();
        });
    }

    _initLimitList(){
        $.map(this.coinListMap, (item, index) => {
            this.levels.forEach((level,index)=>{
                this['limitList_level'+level][item.assetCode] = {
                    limit:null,
                    update:null,
                    isEdit:false
                };
            });
        });
        console.log('initlist is :', this.coinListMap)
    }

    _fetchLimitListByLevel() {
        this.levels.forEach((item)=>{
            let param = {
                key: config['WITHDRAWLEVEL_'+item]
            };

            this.service.configQuery(param).then(rep => {
                this.isLoading = false;
                if(rep.code === window.CODE.SUCCESS){
                    let list = rep.data;
                    let temp = this['limitList_level'+item];
                    $.map(list, (item) => {
                        temp[item.assetCode] = {
                            limit:item.profileValue ,
                            update:null,
                            isEdit:false
                        };
                    });
                }else{
                    window.toast(rep.msg);
                }
            })
        })

    }

    /**
     * 更新每日提币限额
     * @param
     */
    updateLimitByLevel(assetType, updateVal, level) {
        let that = this;

        if ($.trim(updateVal) === '') {
            window.toast(`请输入${assetType}的提币每日限额！`);
            return;
        }

        let modalInstance = this.$uibModal.open({
            animation: false,
            template: passwordDialog,
            scope: this.$scope,
            controller() {
            }
        });

        this.$scope.$$ok = function (value) {
            if (value === '') {
                window.toast('请输入当前账号密码');
                return;
            }

            window.toast('请稍候...');
            that.service.coinConfigUpdate({
                key: config['WITHDRAWLEVEL_'+level],
                asset: assetType,
                value: updateVal
            },{
                'login-password': value
            }).then(rep => {

                if(rep.code === window.CODE.SUCCESS){
                    window.toast(rep.msg);
                    modalInstance.dismiss('ok');
                    that.fetchLimitListByLevel();
                    that['limitList_level'+level][assetType].isEdit = false;
                }else{
                    window.toast(rep.msg);
                }
            })
        };

        this.$scope.$$cancel = function () {
            modalInstance.dismiss('cancel');
        };

        modalInstance.opened.then(() => {
            this.$scope.$$password = '';
        });
    }

    _queryList(callback) {
        let that = this;

        this.isLoading = true;
        // aaa, AAA                     // AAA
        $.when(this.service.coinList(), this.service.coinConfigList()).done((coinListRep, coinConfigListRep) => {
            this.isLoading = false;
            doCoinListRep(coinListRep);
            doCoinConfigListRep(coinConfigListRep);
            if ($.isFunction(callback) === true) {
                callback();
            }
        });

        function doCoinListRep(rep) {
            if (rep.code === window.CODE.SUCCESS) {
                let list = rep.data;
                $.map(list, (item, index) => {
                    that.coinListMap[item.assetCode] = $.extend({}, item);
                    that.coinListMap[item.assetCode].origin = $.extend({}, item);
                });
            } else {
                window.toast(rep.msg);
            }
        }

        function doCoinConfigListRep(rep) {
            if (rep.code === window.CODE.SUCCESS) {
                let list = rep.data;
                $.map(list, (item, index) => {
                    // 以返回的币种列表为准，不存在的则返回
                    if (!that.coinListMap[item.assetCode]) {
                        return
                    }
                    if (item.profileKey === config.ASSETCODECONFIRMNUM) {    // 1.网络确认数
                        that.coinListMap[item.assetCode][config.ASSETCODECONFIRMNUM] = item.profileValue;
                        that.coinListMap[item.assetCode][config.ASSETCODECONFIRMNUM + '_INPUT'] = '';
                    }
                    if (item.profileKey === config.DEPOSITLEVEL_DEFAULT) {    // 2.充值开关
                        that.coinListMap[item.assetCode][config.DEPOSITLEVEL_DEFAULT] = item.profileValue;
                        that.coinListMap[item.assetCode][config.DEPOSITLEVEL_DEFAULT + '_INPUT'] = '';
                    }
                    if (item.profileKey === config.WITHDRAWPRECISION) {    // 4.提现（钱包）精度
                        that.coinListMap[item.assetCode][config.WITHDRAWPRECISION] = item.profileValue;
                        that.coinListMap[item.assetCode][config.WITHDRAWPRECISION + '_INPUT'] = '';
                    }
                    if (item.profileKey === config.WITHDRAWMINFEE) {    // 5.提币手续费
                        that.coinListMap[item.assetCode][config.WITHDRAWMINFEE] = item.profileValue;
                        that.coinListMap[item.assetCode][config.WITHDRAWMINFEE + '_INPUT'] = '';
                    }
                    if (item.profileKey === config.WITHDRAWMIN) {       // 6.提币最小额
                        that.coinListMap[item.assetCode][config.WITHDRAWMIN] = item.profileValue;
                        that.coinListMap[item.assetCode][config.WITHDRAWMIN + '_INPUT'] = '';
                    }
                    if (item.profileKey === config.WITHDRAWMAX) {       // 7.提币最大额
                        that.coinListMap[item.assetCode][config.WITHDRAWMAX] = item.profileValue;
                        that.coinListMap[item.assetCode][config.WITHDRAWMAX + '_INPUT'] = '';
                    } else {
                        that.coinListMap[item.assetCode][item.profileKey] = item.profileValue;
                        that.coinListMap[item.assetCode][item.profileKey + '_INPUT'] = '';
                    }
                });
            } else {
                window.toast(rep.msg);
            }
        }
    }

    /**
     * 查询提币手续费
     */
    queryWithdrawFee() {
        let param = {
            key: config.WITHDRAW_TYPE_FEE
        };
        this.service.configQuery(param).then(rep => {
            if (rep.code === window.CODE.SUCCESS) {
                // this._setWithdrawData(rep.data);
            } else {
                window.toast(rep.msg);
            }
        });
    }

    /**
     * 查询提币最小额度
     */
    queryWithdrawMin() {
        let param = {
            key: config.WITHDRAW_TYPE_MIN
        };
        this.service.configQuery(param).then(rep => {
            if (rep.code === window.CODE.SUCCESS) {
                $.map(rep.data, (item, index) => {
                    this.withdrawMin[item.assetCode] = item.profileValue || 0;
                });
            } else {
                window.toast(rep.msg);
            }
        });
    }

    queryWithdrawMax() {
        let param = {
            key: config.WITHDRAW_TYPE_MAX
        };
        this.service.configQuery(param).then(rep => {
            if (rep.code === window.CODE.SUCCESS) {
                $.map(rep.data, (item, index) => {
                    this.withdrawMax[item.assetCode] = item.profileValue || 0;
                });
            } else {
                window.toast(rep.msg);
            }
        });
    }

    /**
     * 充值列表
     */
    queryDeposit() {
        let param = {
            key: config.DEPOSITLEVEL_DEFAULT
        };
        this.service.configQuery(param).then(rep => {
            if (rep.code === window.CODE.SUCCESS) {
                $.map(rep.data, (item, index) => {
                    this.depositMap[item.assetCode] = item.profileValue;
                });
            } else {
                window.toast(rep.msg);
            }
        });
    }

    update(assetCode, key, value) {
        let that = this;

        confirm('确认要进行该操作吗？', () => {

            let modalInstance = this.$uibModal.open({
                template: passwordDialog,
                scope: this.$scope,
                controller() {
                }
            });

            this.$scope.$$ok = function (password) {
                window.toast('请稍候...');
                that.service.coinConfigUpdate({
                    asset: assetCode,
                    key: key,
                    value: value
                }, {
                    'login-password': password
                }).then(rep => {
                    if(rep.code === window.CODE.SUCCESS){
                        window.toast('操作成功', {
                            callback() {
                                that._queryList();
                            }
                        });
                    }else{
                        window.toast(rep.msg);
                    }
                });
                modalInstance.dismiss('ok');
            };

            this.$scope.$$cancel = function () {
                modalInstance.dismiss('cancel');
            };

        });
    }

    /**
     * 充值更新
     * @param symbol
     * @param status
     * @param operText
     */
    updateDeposit(symbol, status, operText) {
        let that = this;

        confirm('确定要' + operText + symbol + '充值吗?', () => {

            let modalInstance = this.$uibModal.open({
                template: passwordDialog,
                scope: this.$scope,
                controller() {
                }
            });

            this.$scope.$$ok = function (password) {
                window.toast('请稍候...');
                that.service.coinConfigUpdate({
                    key: config.DEPOSITLEVEL_DEFAULT,
                    asset: symbol,
                    value: status
                }, {
                    'login-password': password
                }).then(rep => {
                    if(rep.code === window.CODE.SUCCESS){
                        window.toast('操作成功', {
                            callback() {
                                that._queryList();
                            }
                        });
                    }else{
                        window.toast(rep.msg);
                    }
                });
                modalInstance.dismiss('ok');
            };

            this.$scope.$$cancel = function () {
                modalInstance.dismiss('cancel');
            };

        });
    }

    updateStatus(item, status, operText) {
        let that = this;

        let param = $.extend({}, item.origin, {
            status: status
        });

        confirm('确定要' + operText + item.assetCode + '功能吗?', () => {

            let modalInstance = this.$uibModal.open({
                template: passwordDialog,
                scope: this.$scope,
                controller() {
                }
            });

            this.$scope.$$ok = function (password) {
                window.toast('请稍候...');
                that.service.coinAdd(param, {
                    'login-password': password
                }).then(rep => {
                    if(rep.code === window.CODE.SUCCESS){
                        window.toast('操作成功', {
                            callback() {
                                that._queryList();
                            }
                        });
                    }else{
                        window.toast(rep.msg);
                    }
                });
                modalInstance.dismiss('ok');
            };

            this.$scope.$$cancel = function () {
                modalInstance.dismiss('cancel');
            };

        });
    }

    /**
     * 更新提币手续费
     * @param symbol
     */
    updateWithdrawFee(symbol) {

        let value = this.withdrawInput[symbol];
        let that = this;

        if ($.trim(value) === '') {
            window.toast(`请输入${symbol}的提币手续费！`);
            return;
        }

        if (value < 0) {
            window.toast(`${symbol}的提币手续费必须 > 0`, {timeout: 4000});
            return;
        }

        let modalInstance = this.$uibModal.open({
            template: passwordDialog,
            scope: this.$scope,
            controller() {
            }
        });

        this.$scope.$$ok = function (password) {
            if (value === '') {
                window.toast('请输入当前账号密码');
                return;
            }
            let param = {
                key: config.WITHDRAW_TYPE_FEE,
                asset: symbol,
                value: value
            };
            window.toast('请稍候...');
            that.service.coinConfigUpdate(param, {
                'login-password': password
            }).then(rep => {
                if(rep.code === window.CODE.SUCCESS){
                    window.toast(rep.msg);
                    that.withdrawInput[symbol] = '';
                    that.withdrawFee[symbol] = value;
                }else{
                    window.toast(rep.msg);
                }
            });
            modalInstance.dismiss('ok');
        };

        this.$scope.$$cancel = function () {
            modalInstance.dismiss('cancel');
        };

    }

    /**
     *
     * @param symbol
     */
    updateWithdrawMin(symbol) {

        let value = this.withdrawMinInput[symbol];
        let that = this;

        if ($.trim(value) === '') {
            window.toast(`请输入${symbol}的提币最小额度！`);
            return;
        }

        value = +value;

        if (value <= +this.withdrawFee[symbol]) {
            window.toast(`${symbol}的提币最小额度必须 > ${symbol}的提币手续费`, {timeout: 4000});
            return;
        }

        if (value >= +this.withdrawMax[symbol]) {
            window.toast(`${symbol}的提币最小额度必须 < ${symbol}的提币最大额度`, {timeout: 4000});
            return;
        }

        let modalInstance = this.$uibModal.open({
            template: passwordDialog,
            scope: this.$scope,
            controller() {
            }
        });

        this.$scope.$$ok = function (password) {
            if (value === '') {
                window.toast('请输入当前账号密码');
                return;
            }
            let param = {
                key: config.WITHDRAW_TYPE_MIN,
                asset: symbol,
                value: value
            };
            window.toast('请稍候...');
            that.service.coinConfigUpdate(param, {
                'login-password': password
            }).then(rep => {
                if(rep.code === window.CODE.SUCCESS){
                    window.toast(rep.msg);
                    that.withdrawMinInput[symbol] = '';
                    that.withdrawMin[symbol] = value;
                }else{
                    window.toast(rep.msg);
                }
            });
            modalInstance.dismiss('ok');
        };

        this.$scope.$$cancel = function () {
            modalInstance.dismiss('cancel');
        };

    }

    /**
     *
     * @param symbol
     */
    updateWithdrawMax(symbol) {

        let value = this.withdrawMaxInput[symbol];
        let that = this;

        if ($.trim(value) === '') {
            window.toast(`请输入${symbol}的提币最大额度！`);
            return;
        }

        value = +value;

        if (value <= +this.withdrawMin[symbol]) {
            window.toast(`${symbol}的提币最大额度必须 > ${symbol}的提币最小额度`, {timeout: 4000});
            return;
        }

        let modalInstance = this.$uibModal.open({
            template: passwordDialog,
            scope: this.$scope,
            controller() {
            }
        });

        this.$scope.$$ok = function (password) {
            if (value === '') {
                window.toast('请输入当前账号密码');
                return;
            }
            let param = {
                key: config.WITHDRAW_TYPE_MAX,
                asset: symbol,
                value: value
            };
            window.toast('请稍候...');
            that.service.coinConfigUpdate(param, {
                'login-password': password
            }).then(rep => {
                if(rep.code === window.CODE.SUCCESS){
                    window.toast(rep.msg);
                    that.withdrawMaxInput[symbol] = '';
                    that.withdrawMax[symbol] = value;
                }else{
                    window.toast(rep.msg);
                }
            });
            modalInstance.dismiss('ok');
        };

        this.$scope.$$cancel = function () {
            modalInstance.dismiss('cancel');
        };

    }
}


export default ListController;
